﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using DBControl;
using System.Web.Security;

namespace ProjectX.AjaxCommand
{
    /// <summary>
    /// UpdatePass 的摘要说明
    /// </summary>
    public class UpdatePass : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/plain";
            string getold = "SELECT passWord FROM UserRegister WHERE userName = '" + context.Session["userNick"] + "'";
            string oldpass = FormsAuthentication.HashPasswordForStoringInConfigFile(context.Request.Form["oldpass"], "MD5"); ;
            string newpass = FormsAuthentication.HashPasswordForStoringInConfigFile(context.Request.Form["newpass"], "MD5");
            string uppass = "UPDATE UserRegister SET passWord = '" + newpass + "' WHERE userName = '" + context.Session["userNick"] + "'";
            SqlDbHelper sdh = new SqlDbHelper();
            try
            {
                if (oldpass.Equals(sdh.ExecuteScalar(getold)))
                {
                    if (sdh.ExecuteNonQuery(uppass) != 0)
                    {
                        context.Response.Write("ok");
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("");
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("olderror");
                    context.Response.End();
                }
            }
            catch (Exception)
            {
                context.Response.Write("");
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}